# 用于将某个目录下的多个日志文件合并为一个文件，按文件名排序，所以使用前需要将日志文件按时间顺序重命名
# 使用下面bash command 删除开机号日期为0101的行
# grep -v "I0101" merged_log.log > output.log

import os

# specify the directory where the log files are located
log_dir = "/home/hanxiao/wspace/docker_src/sfc-src/build_ninja/deploy/xcthuj0326/log/qsc_ack_fix/"

# specify the name of the merged log file
merged_log_file = log_dir + "qsc_ack_fix.log"

# function to merge multiple log files into one file
def merge_logs(log_dir, merged_log_file):
    # create a list of all log files in the specified directory
    log_files = [os.path.join(log_dir, f) for f in os.listdir(log_dir) if os.path.isfile(os.path.join(log_dir, f))]

    # sort the list of log files by name
    log_files.sort()
    
    with open(merged_log_file, "w") as merged_log:
        # loop through each log file and append its contents to the merged log file
        for log_file in log_files:
            with open(log_file, "r") as log:
                merged_log.write(log.read())


# call the merge_logs function to merge the log files
merge_logs(log_dir, merged_log_file)

# print a message to indicate that the merge is complete
print("Log merge complete!")
